package org.batyrov.equery.action.admin;

import org.batyrov.equery.action.BaseAction;
import org.batyrov.equery.action.Const;
import org.batyrov.equery.bean.Service;
import org.batyrov.equery.bean.Window;

import javax.persistence.EntityTransaction;
import java.util.Collection;

/**
 * Created by IntelliJ IDEA.
 * User: sulic
 * Date: 27.02.2010
 * Time: 19:13:16
 * To change this template use File | Settings | File Templates.
 */
public class UnLinkWindowService extends BaseAction {
    private long windowId = -1;
    private long serviceId = -1;
    private String serviceName;
    private String windowName;

    public long getWindowId() {
        return windowId;
    }

    public void setWindowId(long windowId) {
        this.windowId = windowId;
    }

    public long getServiceId() {
        return serviceId;
    }

    public void setServiceId(long serviceId) {
        this.serviceId = serviceId;
    }

    public String getServiceName() {
        return serviceName;
    }

    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }

    public String getWindowName() {
        return windowName;
    }

    public void setWindowName(String windowName) {
        this.windowName = windowName;
    }

    

    @Override
    public String input() throws Exception {
        return super.input();    
    }

    @Override
    public String execute() throws Exception {
        try {
            super.execute();
            Service service = em.find(Service.class, serviceId);
            if (service == null) {
                addActionError(getText("cannot_found_service"));
                return Const.ERROR;
            }
            Window window = em.find(Window.class, windowId);
            if (window == null) {
                addActionError(getText("cannot_found_window"));
                return Const.ERROR;
            }
            EntityTransaction tx = em.getTransaction();
            try {
                tx.begin();
                service.getWindows().remove(window);
                tx.commit();
            } catch (Exception e) {
                if (tx.isActive()) {
                    tx.rollback();
                }
            }
            return Const.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            addActionError(e.toString());
            return Const.ERROR;
        }
    }
}
